#ifndef NET_TCP_STREAM_H_
#define NET_TCP_STREAM_H_

#include "base/base.h"
#include "net/poller.h"

namespace net {
struct Addr;
class Socket;
class Buffer;
class Processor;

class TcpStream {
  public:
    explicit TcpStream(Socket* sock, Poller* poller);
    ~TcpStream();

    bool Init();

    void SetProcessor(Processor* processor) {
      processor_ = processor;
    }
    void SetCloseClosure(Closure* closure) {
      close_closure_.reset(closure);
    }
    void SetWriteCompleteClosure(Closure* closure) {
      write_complete_closure_ = closure;
    }

    int32 Fd();

    void Send(const u_char* buf, size_t len, Closure* closure);

    static void HandleEvent(int fd, void* arg, int event);

    void HandleRead();
    void HandleWrite();
    void HandleClose();

  private:
    Poller* poller_;
    scoped_ptr<Socket> sock_;
    scoped_ptr<Buffer> input_buf_;
    scoped_ptr<Buffer> output_buf_;

    Event ev_;

    Processor* processor_;
    scoped_ptr<Closure> close_closure_;

    Closure* write_complete_closure_;

    DISALLOW_COPY_AND_ASSIGN(TcpStream);
};
}

#endif
